import datetime
import json
import os

import pandas as pd

from utility.utility_func import *
file_path = os.path.abspath("..\\parameters\\parameters_future.json")




def get_future_parameters(symbol):
    pass

    with open(file_path) as f:
        future_parameter = json.load(f)

    try:
        result = future_parameter.get(symbol)
        return (result)
    except:
        msg = f"cannot get parameter for {symbol}"
        print(msg)
        return None


def get_ZL_months(symbol,start_date =  datetime.datetime(year=2013,month=1,day=1),end_date = datetime.datetime.now()):
    parameter = get_future_parameters(symbol)
    start_year = start_date.year
    end_year = end_date.year
    year = start_year
    ZL_months = parameter.get("main_months")
    trading_months = []
    while year<= end_year:
        for month in ZL_months:
            if len(str(month)) ==1:
                year_month = str(year)[2:4] + "0" + str(month)
            else:
                year_month = str(year)[2:4] + str(month)
            trading_months.append(year_month)

        year+=1

    return trading_months

def load_data(file_path):
    names = ["exchang","instrument","datetime","open","high","low","close","volume","turnover","open_interest"]
    raw_data = pd.read_csv(file_path,encoding="gb2312",header=0,names = names,parse_dates=["datetime"],index_col="datetime")
    return raw_data

def generate_future_ZL(symbol,data_path):
    # LME相关品种，交割前一个月月底
    # 其他金属， 交割前两个月月底
    # 农产品
    trading_months = get_ZL_months(symbol)
    main_gap = get_future_parameters(symbol).get("main_gap")

    result = []

    for year_month in trading_months:
        year = int("20" + year_month[0:2])
        month = int(year_month[2:4])


        end_date = datetime.datetime(year=year,month =month,day=1) - datetime.timedelta(days= 31 * main_gap + 1)
        row ={"symbol":symbol,
              "instrument": symbol + year_month,
              "end_date":end_date
              }
        result.append(row)

    result = pd.DataFrame(result)

    data_set = {}
    full_data = pd.DataFrame()

    for i in range(2,len(result)):
        start_date = result.loc[i-1].end_date + datetime.timedelta(days=1)
        end_date = result.loc[i].end_date
        instrument = result.loc[i].instrument
        if start_date.year == end_date.year:
            file_path = data_path + f"FutAC_Min1_Std_{str(start_date.year)}\\"+instrument+".csv"
            data = load_data(file_path)
            data = data[start_date:end_date]
            data_set[instrument] = data
            full_data = pd.concat([full_data,data])
        else:
            file_path = data_path + f"FutAC_Min1_Std_{str(start_date.year)}\\"+instrument+".csv"
            data1 = load_data(file_path)
            file_path = data_path + f"FutAC_Min1_Std_{str(end_date.year)}\\"+instrument+".csv"
            data2 = load_data(file_path)
            data = pd.concat([data1,data2])[start_date:end_date]
            data_set[instrument] = data
            full_data = pd.concat([full_data,data])

    return [result,data_set,full_data]


if __name__ == '__main__':
    symbol = "rb"

    # file_path = "C:\\data\\futures\\FutAC_Min1_Std_2013\\rb1310.csv"
    # data = load_data(file_path)
    # print(data)
    data_path = f"C:\\data\\futures\\"

    full_data = generate_future_ZL(symbol,data_path)
    print(full_data)
